﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Utils
{
    public static class RandomUtil
    {
        /// <summary>
        /// 该方法用于生成指定位数的随机数
        /// </summary>
        /// <param name="len">参数是随机数的位数</param>
        /// <returns>返回一个随机数字符串</returns>
        public static string GetLetterOrDigit(int len)
        {
            string[] source = { "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            return CreateCode(source, len);
        }
        /// <summary>
        /// 产生随机字符串
        /// </summary>
        /// <param name="len">随机生成数字</param>
        /// <returns>随机出的字符串</returns>
        public static string GetDigit(int len)
        {
            string[] source = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            return CreateCode(source, len);
        }

        /// <summary>
        /// 产生随机字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string CreateCode(string[] source, int len)
        {
            string code = string.Empty;
            long tick = DateTime.Now.Ticks;
            Random rd = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            int i;
            for (i = 0; i < len; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }


    }
}
